#!/bin/bash
#################################################################################
#
# Scripts compiles various components of the model based on flags specified
#
#################################################################################
function runmake {
 local flags=
 local pflag=serial
 while [ "$1" != "" ]; do
  if [ "$1" == "debug" ]; then
   export DEBUG="true"
  elif [ "$1" == "optimized" ]; then
   export DEBUG="";
  elif [ "$1" == "parallel" ]; then
   flags="$1";
   pflag=parallel;
  elif [ "$1" == "serial" ]; then
   flags="$1";
  elif [ "$1" == "clean" ]; then
   flags="clean"
  fi
  shift;
 done;
 if [ "$DEBUG" == "true" ]; then
   echo 'Compiling debug mode.' | tee ${pflag}.compile.log | tee $ROUT
 fi
 echo Trying \'make ${flags}\' | tee "${pflag}".compile.log | tee $ROUT 
 make ${flags} 2>&1 | tee "${pflag}".compile.log | tee $ROUT > /dev/null
 return $?;
};
####################################################################
function add_type {
 local new_type="$1";
 echo ${maketypes} | grep "${new_type}" &> /dev/null
 if [ $? -ne 0 ]; then # type needs to be added
   echo ${maketypes} "${new_type}";
 else
   echo ${maketypes}
 fi
 return 0;
};
####################################################################
# global flags
#
DEBUG=
maketypes=
makeflags=
paroptions=
####################################################################
# read all args
while [ "$1" != "" ]; do
  if [ "$1" == "debug" ]; then
    maketypes=$(add_type "debug");
  elif [ "$1" == "opt" ] || [ "$1" == "optimized" ]; then
    maketypes=$(add_type "optimized");
  elif [ "$1" == "all" ]; then
    maketypes=$(add_type "optimized");
    maketypes=$(add_type "debug");
  elif [ "$1" == "clean" ]; then
    makeflags=clean
  elif [ "$1" == "par" ] || [ "$1" == "parallel" ]; then
     paroptions="$paroptions parallel" 
  elif [ "$1" == "ser" ] || [ "$1" == "serial" ]; then
     paroptions="$paroptions serial"
  else
     echo $(basename $0)": unknown compilation type/option $1." > /dev/stderr
     exit 1;
  fi
 shift
done;
if [ "$paroptions" == "" ]; then
   paroptions=serial
fi
if [ "$maketypes" == "" ]; then
   maketypes=$(add_type "optimized");
fi
####################################################################
# check to see if rout terminal is active
pushd ./ &> /dev/null
cd ./rout/ 
if ! make install; then
 echo $(basename $0)": could not create remote terminal for compilation." > /dev/stderr;
 exit 1;
fi
cd ..

iter=0
ROUT=$(rout)
if [ "${ROUT}" == "" ]; then
  start-rout-terminal localhost & 
  while [ "${ROUT}" == "" ]; do
    ROUT=$(rout);
    sleep 0.5;
    iter=$(($iter+1))
    if [ $iter -gt 500 ]; then
	echo $(basename $0)": failed to create remote terminal for compilation." > /dev/stderr; exit 1;
    fi
  done
fi
####################################################################
# Run make for all types requested
####################################################################
for opt in $paroptions; do
for type in $maketypes; do
 runmake $type $opt $makeflags
done;
done;
####################################################################
exit 0;


